package com.lingyistudio.common.util;

import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @ClassName: Md5AndSha1
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/25
 */
@Slf4j
public class Md5AndSha1 {
    private static ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();

    //plaintext:明文密码 ciphertext：密文
    public static boolean verifyMD5AndSHA1(String plainText, String cipherText)  {
        //根据传入的密钥进行验证
        int textint = Integer.parseInt(cipherText.substring(cipherText.length()-1));
        String strLast = cipherText.substring(40,cipherText.length()-1);
        String plainTextMd5 = getMd5(plainText+strLast)+plainText;
        String str="";
        for(int i=0;str.length()>15;i++){
            str=str+plainTextMd5.charAt( plainTextMd5.length()%(i*textint) );
        }
        if( (getSHA1(str+plainText)+strLast+textint).equals(cipherText) ){
            return true;
        }
        return false;
    }

    //MD5和SHA1混合加密，plainText：明文密码
    public static String randomMD5AndSHA1(String plainText)  {
        int randomint = threadLocalRandom.nextInt(1,9);
        char[] randomchar = {(char) threadLocalRandom.nextInt(1,65534),
                (char)threadLocalRandom.nextInt(1,65534),(char)threadLocalRandom.nextInt(1,65534),
                (char)threadLocalRandom.nextInt(1,65534),(char)threadLocalRandom.nextInt(1,65534),
                (char)threadLocalRandom.nextInt(1,65534),(char)threadLocalRandom.nextInt(1,65534),
                (char)threadLocalRandom.nextInt(1,65534)};
        String randomString =String.valueOf(randomchar);
        String ciphertextMD5 = getMd5(plainText+randomString)+plainText;
        String str="";
        for(int i=0;str.length()>15;i++){
            str=str+ciphertextMD5.charAt( ciphertextMD5.length()%(i*randomint) );
        }
        return getSHA1(str+plainText)+randomString+randomint;
    }
    private static String getMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes("UTF-8"));
            byte byteArray[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0;offset <byteArray.length;offset++){
                i = byteArray[offset];
                if (i<0) {
                    i += 256;
                }
                if (i<16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            //32位加密
            return buf.toString();
        } catch (Exception e){
            log.error("common.util.Md5AndSha1.getMd5  "+String.valueOf(e));
            e.printStackTrace();
            return null;
        }
    }
    private static String getSHA1(String plaintext){
        try {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            byte[] byteArray;
            byteArray = plaintext.getBytes("UTF-8");
            byte[] md5Bytes = sha.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            log.error("common.util.Md5AndSha1.getSHA1  "+String.valueOf(e));
            e.printStackTrace();
            return null;
        }
    }
}
